<?php /** @noinspection ALL */

//任务类型

namespace app\admin\controller;


use think\facade\Db;

class TaskType {

	function post(){
		$input=input();

		if (empty($input['cycle'])) return json(['code' => 0, 'msg' => '请输入周期']);

		if (empty($input['end_time'])) return json(['code' => 0, 'msg' => '请输入停售时间']);

		$data=[
			'cycle'=>$input['cycle'],
			'end_time'=>$input['end_time'],
			'add_time'=>time(),
		];

		if(empty($input['id'])){

			$r=db::name('task_type')->insert($data);

			if (!$r){
				return json(['code'=>0,'msg'=>'编辑失败']);
			}

		}else{

			$r=db::name('task_type')->where('id',$input['id'])->update($data);

			if (!$r){
				return json(['code'=>0,'msg'=>'编辑失败']);
			}
		}
		return json(['code'=>1,'msg'=>'编辑成功']);

	}

	function del(){
		$input = input();

		if (!$input['id']) return json(['code' => 0, 'msg' => '数据错误']);

		$r = Db::table('task_type')->where('id', $input['id'])->find();

		if (empty($r)) return json(['code' => 0, 'msg' => '数据不存在']);

		$re = Db::name('task_type')->where('id', $input['id'])->delete();

		if (!$re) {
			return json(['code' => 0, 'msg' => '删除失败']);
		}
		return json(['code' => 1, 'msg' => '删除成功']);
	}

	function show($page=1,$pageSize=10){

		$input=input();

		$list = Db::query("select *,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s')  as add_time from task_type order by id desc limit ".(($page-1)*$pageSize).",$pageSize");

		$total_num=Db::query("select count(id) as sum from task_type");

		return json(['code' => 1, 'data' => $list,'total_num'=>$total_num[0]['sum']]);
	}

}